package com.rljk.controller.app;

import com.rljk.common.consts.OrderEnum;
import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.TaskManager;
import com.rljk.service.UserManager;
import com.rljk.service.impl.OrderService;
import com.rljk.utils.AppUtil;
import com.rljk.utils.RelativeDateFormat;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   YASUO
 * Date:     2018/7/13 14:53
 * Description:
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderInController extends BaseController{

    @Resource(name = "orderService")
    private OrderService orderService;

    @Autowired
    RedisService redisService;

    /**
     * app购物中心获取订单信息
     * @Description
     * @Author zhaozhenhua
     * @date   2018年7月13日
     */
    @RequestMapping(value="/shopcenter")
    //@Encrypt
    public Object getOrderInfo() {
        log.info("app购物中心获取订单信息");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("ORDERINFO", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getOrderInfo",pd)){
                    //1.获取用户信息
                    String token = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",token);
                    Integer status = null;
                    if(Tools.notEmpty(pd.getString("STATUS"))){
                         status = pd.getInterger("STATUS");
                    }
                    List<PageData> list =  orderService.getOrderInfo(pd);
                    if(Tools.isEmptyList(list)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    for (PageData p : list) {
                        p.put("STATUS", OrderEnum.getName(p.getInterger("STATUS")));
                    }
                    map.put("list",list);
                    return Result.success(map);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }
}
